 /*
 * 文 件 名:  ResourceDAOImpl.java
 * 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2012,  All rights reserved
 * 描    述:  资源操作接口实现
 * 修 改 人:  aganzhoul
 * 修改时间:  2012-9-20
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.tydic.simple.dao.user.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.tydic.framework.dao.AbstractIbatisDAO;
import com.tydic.framework.dataobject.ResultDTO;
import com.tydic.framework.exception.DAOException;
import com.tydic.simple.dao.user.IResourceDAO;
import com.tydic.simple.dto.user.ResDTO;
import com.tydic.simple.model.user.Resource;

 /**
 * 资源操作接口实现
 * 
 * @author  aganzhoul
* @version  [1.0, 2012-9-20]
* @since  [权限管理/1.0]
 */
public class ResourceDAOImpl extends AbstractIbatisDAO implements IResourceDAO {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceDAOImpl.class);
    
    /** {@inheritDoc} */
    @Override
    public Long saveResource(Resource resource)
        throws DAOException {
        try {
            sqlMapClient.insert("user.INSERT_RESOURCE", resource);
        }
        catch (SQLException e) {
            LOGGER.error("saveResource exception",e);
            throw new DAOException("saveResource exception", e);
        }
        return resource.getResId();
    }
    
    /** {@inheritDoc} */
    @Override
    public int deleteResByCode(String resourceCode)
        throws DAOException {
        try {
            return sqlMapClient.delete("user.DELETE_RES_BY_CODE", resourceCode);
        }
        catch (SQLException e) {
            LOGGER.error("deleteResByCode exception",e);
            throw new DAOException("deleteResByCode exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @Override
    public Resource findResByCode(String resourceCode)
        throws DAOException {
        try {
            return (Resource)sqlMapClient.queryForObject("user.FIND_RES_BY_CODE", resourceCode);
        }
        catch (SQLException e) {
            LOGGER.error("findResByCode exception",e);
            throw new DAOException("findResByCode exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public ResultDTO<Resource> findResourceForPage(ResDTO resDTO)
        throws DAOException {
        ResultDTO<Resource> result = new ResultDTO<Resource>();
        try {
            result.setResult(sqlMapClient.queryForList("user.FIND_RESOURCE_PAGE", resDTO));
            BeanUtils.copyProperties(resDTO.getPagination(), result.getPagination());
            result.getPagination()
                .setTotal((Integer)sqlMapClient.queryForObject("user.FIND_RESOURCE_PAGE_COUNT", resDTO));
        }
        catch (SQLException e) {
            LOGGER.error("findResource exception",e);
            throw new DAOException("findResource exception", e);
        }
        return result;
    }

     /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public List<Resource> findResourceForList(ResDTO resDTO)
        throws DAOException {
        try {
            List<Resource> resourceList = sqlMapClient.queryForList("user.FIND_RESOURCE", resDTO);
            return resourceList != null ? resourceList : new ArrayList<Resource>();
        }
        catch (SQLException e) {
            LOGGER.error("findResourceForList exception",e);
            throw new DAOException("findResourceForList exception", e);
        }
    }

     /** {@inheritDoc} */
    @Override
    public Resource findResourceForSingle(ResDTO resDTO)
        throws DAOException {
        List<Resource> resourceList = findResourceForList(resDTO);
        return resourceList.size() > 0 ? resourceList.get(0) : null;
    }

     /** {@inheritDoc} */
    @Override
    public int updateResource(Resource resource)
        throws DAOException {
        try {
            return sqlMapClient.update("user.UPDATE_RESOURCE_BY_CODE", resource);
        }
        catch (SQLException e) {
            LOGGER.error("updateResource exception",e);
            throw new DAOException("updateResource exception", e);
        }
    }
    
}
